/*script.newObject("test", "TestPlug");
test.sayHello("works?");
test.sayGoodbye("works!!");
script.deleteObject("test");*/

script.newObject("authreq", "WebRequest");
authreq.requestFinished.connect(authRequestFinished);
authreq.get("http://rghost.net/multiple/upload_host", 10000);

function authRequestFinished() {
	var resp = eval("(" + authreq.getContents() + ")"); //parse JSON answer
	var uphost = resp["upload_host"];
	var authcode = resp["authenticity_token"];
	if(authcode.length && uphost.length) {
		script.newObject("data", "PostData");
		data.appendField("authenticity_token", authcode, false)
		data.appendFile("file", script.filepath()/*"D:\\snapshot20080505011346.jpg.bmp"*/, "application/octet-stream", false);
		script.newObject("request", "Request");
		request.setUrl("http://" + uphost + "/files");
		//request.setUrl("http://lab10/upload.php");
		request.setHeader("Content-Type", "multipart/form-data");
		request.setHeader("Cookie", authreq.getHeader("Set-Cookie"));
		script.newObject("uploadreq", "WebRequest");
		uploadreq.requestFinished.connect(uploadRequestFinished);
		uploadreq.post(request, data, 30000);
		script.deleteObject("request");
	} else script.failed("Can't get auth information from server");
	script.deleteObject("authreq");
}

function uploadRequestFinished() {
	script.succeeded("Everything is ok: " + uploadreq.getUrl());
	script.deleteObject("data");
	script.deleteObject("uploadreq");
}